var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dis: true, //判断第一题
    cid: 0,
    // 下一题和分享
    nextdis: false,
    nexttil: 0,

    //   选择答案
    selects: '-1',
    quest_selects: 0,
    // 题目内容
    topic: '我最喜欢的旅游出行方式是?',
    lis: '1/10',
    quest: [],
    selects_box: [],//答案数组
  },


  // 分享和提交答案
  sharebtn: function (e) {
    var that = this;
    var selects_box = that.data.selects_box;
    var selects_id_box = [];
    for (var i in selects_box) {
      selects_id_box.push(selects_box[i].id);
    }
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 3000
    }),
      wx.request({
        url: app.url + 'api/setquestion',
        method: 'POST',
        data: {
          selects_box: selects_id_box,
          openid: wx.getStorageSync('openid'),
          avatarurl: wx.getStorageSync('avatarurl'),
          tid: that.data.tid,
          cid: that.data.cid,
        },
        header: {
          'content-type': 'application/json',
        },
        success: function (res) {
          wx.redirectTo({
            url: '../share/index?code_url=' + res.data.data.code_url + '&cid=' + res.data.data.cid,
          })
        },
      })
  },



  // 下一题
  passbtn: function (e) {
    var that = this;
    var quest_selects = e.currentTarget.dataset.quest_selects;//选题
    var quest_box = that.data.quest_box; //题库
    var selects_box = that.data.selects_box; //答案库
    if (selects_box[quest_selects] != undefined) {//判断当前题目用户是否选择答案
      console.log(333);
      that.setData({
        dis: false, nextdis: true,        
        quest_selects: quest_selects,
      })
      console.log(555);
      ++quest_selects;
      if (quest_box[quest_selects] != undefined) {//判断下一题是否还有题目给用户作答
        console.log(quest_box[quest_selects]);
        //判断下一题用户是否有做答
        if (selects_box[quest_selects] != undefined) {//如果有
          for (var i in quest_box) {
            if (quest_box[i].title == selects_box[quest_selects].title && quest_box[i].title) {
              for (var a in quest_box[i].text) {
                if (quest_box[i].text[a].id == selects_box[quest_selects].id) {
                  that.setData({
                    dis: false, nextdis: true,
                    selects: a,
                    quest_selects: quest_selects,
                  })
                }
              }
            }
          }
        } else {//如果没有
          that.setData({
            dis: false, nextdis: true,
            quest_selects: quest_selects,
            selects: '-1',
          })
        }
      } else {
        var selects = '-1';
        if (quest_box.length == quest_selects) {
          for (var i in selects_box) {
            for (var g in quest_box[quest_selects - 1].text) {
              if (selects_box[i].id == quest_box[quest_selects - 1].text[g].id) {
                selects = g;
              }
            }
          }

        }
        that.setData({
          nexttil: 1,
          selects: selects,
        })
      }
    } else {
      wx.showToast({
        title: '请选择答案',
        icon: 'loading',
        duration: 1200
      })
    }
  },




  // 上一题
  firston: function (e) {
    var that = this;
    var selects_box = that.data.selects_box; //答案库
    var quest_box = that.data.quest_box; //题库
    var quest_selects = e.currentTarget.dataset.quest_selects;//选题
    --quest_selects;  //直接下一题
    for (var i in quest_box) {
      if (quest_box[i].title == selects_box[quest_selects].title && quest_box[i].title) {
        for (var a in quest_box[i].text) {
          if (quest_box[i].text[a].id == selects_box[quest_selects].id) {
            that.setData({
              selects: quest_selects,
            })
          }
        }
      }
    }

    --quest_selects; //继续下一题
    if (that.data.quest_box[quest_selects] != undefined) {
      selects();
      that.setData({
        nexttil: 0,
        quest_selects: ++quest_selects,
      })
    } else {
      selects();
      that.setData({
        dis: true,
        nextdis: false,
        quest_selects: ++quest_selects,
      })
    }

    function selects() {
      for (var i in quest_box[quest_selects + 1].text) {
        if (quest_box[quest_selects + 1].text[i].id == selects_box[quest_selects + 1].id) {
          that.setData({
            selects: i,
          })
        }
      }
    }
  },
  //选着答案
  change: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var title = e.currentTarget.dataset.title;
    var selects_box = that.data.selects_box;
    if (selects_box[index] == undefined) {
      selects_box.push({ 'id': id, 'title': title });
    } else {
      selects_box[index].id = id;
    }

    that.setData({
      selects_box: selects_box,
    })
  },
  // 选中id框变色
  selted: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.id;
    var selects = that.data.selects;
    that.setData({
      selects: e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({ title: 'loading', mask: true })
    var that = this;
    if (options.cid) {
      that.setData({ tid: options.id, cid: options.cid });
    } else {
      that.setData({ tid: options.id });
    }
    wx.request({
      url: app.url + 'api/loadquestion',
      data: {
        openid: wx.getStorageSync('openid'),
        tid: options.id,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var data = res.data;
        that.setData({
          quest_box: res.data,
        })
        wx.hideLoading();
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function prompt(text, stat) {
  //提示信息
  wx.showToast({
    title: text,
    icon: stat,
    duration: 1200,
  })
};